﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Slam.SharePoint
{
    /// <summary>
    /// Extension class for SPContentType.
    /// </summary>
    public static class SPContentTypeExtender
    {
        /// <summary>
        /// Extension method for evaluating whether the given SPContentType is equal to this SPContentType or is
        /// one of its parents.
        /// </summary>
        /// <param name="thisContentType"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static bool EqualsOrInherits(this SPContentType thisContentType, SPContentType contentType)
        {
            bool isFound = thisContentType.Name == contentType.Name;

            //follow parents until the Parent is "System".  Everything inherits System and it is
            //nonsensical if System is trying to be matched
            if (!isFound && thisContentType.Parent.Name.ToLower() != "system")
            {
                if (thisContentType.Parent.Name == contentType.Name)
                {
                    isFound = true;
                }
                else
                {
                    isFound = thisContentType.Parent.EqualsOrInherits(contentType);
                }
            }
            return isFound;
        }

        /// <summary>
        /// Extension method for evaluating whether the given content type name is the name of this SPContentType or is
        /// one of its parents.
        /// </summary>
        /// <param name="thisContentType"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public static bool EqualsOrInherits(this SPContentType thisContentType, string contentType)
        {
            bool isFound = thisContentType.Name == contentType;

            //follow parents until the Parent is "System".  Everything inherits System and it is
            //nonsensical if System is trying to be matched
            if (!isFound && thisContentType.Parent.Name.ToLower() != "system")
            {
                if (thisContentType.Parent.Name == contentType)
                {
                    isFound = true;
                }
                else
                {
                    isFound = thisContentType.Parent.EqualsOrInherits(contentType);
                }
            }
            return isFound;
        }
    }
}
